<?php

namespace app\admin\model;

use think\Model;
use think\Db;

class Warehousing extends Model
{

    

    

    // 表名
    protected $name = 'warehousing';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'good_json_text',
        'create_time_text',
    ];

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }
    public function getGoodJsonTextAttr($value, $data)
    {
        if($data){
            $return = json_decode($data['good_json'],true);
            foreach ($return as $key => $value) {
                $cangku_data = Db::name('cangku')->where('id',$value['cangku_id'])->find();
                $return[$key]['cangku_data'] = $cangku_data;
            }
            return $return;
        }
    }
    protected static function init()
    {
        self::afterInsert(function ($row) {
            $good_json = $row->good_json;
            $good_array = json_decode($good_json,true);
            $createtime = time();
            $insertall = [];
            foreach ($good_array as $key => $value) {

                $if_exist = Db::name('rugood')->where(['sku'=>$value['name']])->find();

                if($if_exist){
                    Db::name('rugood')->where(['sku'=>$value['name']])->setInc('in_num',$value['num']);//总入库
                    Db::name('rugood')->where(['sku'=>$value['name']])->setInc('not_in_num',$value['not_in_num']);
                    Db::name('rugood')->where(['sku'=>$value['name']])->setInc('shengyu_num',$value['num']);
                }else{
                    $insertall['createtime'] = $createtime;
                    $insertall['sku'] = $value['name'];
                    $insertall['in_num'] = $value['num'];
                    $insertall['location'] = $value['location'];
                    $insertall['not_in_num'] = $value['not_in_num'];
                    $insertall['batch_number'] = $value['batch_number'];
                    $insertall['shengyu_num'] = $value['num'];
                    $insertall['cangku_id'] = $value['cangku_id'];
                    $insertall['type'] = $value['type'];
                    $insertall['mianshui'] = $value['mianshui'];
                    $insertall['huohao'] = $value['huohao'];
                    $insertall['codebar'] = $value['codebar'];
                    $insertall['zhekou'] = $value['zhekou'];
                    $insertall['channel_source'] = $value['channel_source'];

                    $res = Db::name('rugood')->insert($insertall);
                    // halt($res);
                }
                

            }

            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk],'order_no'=>'RK'.date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8)]
            );
        });
    }

        public function admin()
    {
        return $this->belongsTo('admin', 'user_id')->bind('nickname');
    }


    







}
